package com.dst.smart.dao.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.dst.smart.dao.impl.factory.DeviceFactory;
import com.dst.smart.model.ActionParam;
import com.dst.smart.model.Arce;
import com.dst.smart.model.CusDevice;
import com.dst.smart.model.Device;
import com.dst.smart.model.DeviceAction;
import com.dst.smart.model.DeviceFunction;
import com.dst.smart.model.DeviceType;
import com.dst.smart.model.FunctionParam;
import com.dst.smart.model.GateWay;
import com.dst.smart.model.GatewayType;
import com.dst.smart.model.Mode;
import com.dst.smart.model.ModeList;
import com.dst.smart.model.Products;
import com.dst.smart.model.RedInfra;
import com.dst.smart.model.Room;
import com.dst.smart.model.SubscribeInfo;
import com.dst.smart.model.VoiceInfo;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

@SuppressLint("SimpleDateFormat")
public class WidgetDAOImpl implements IWidgetDAO {
	public static final String TABLE_ROOM = "t_room";
	public static final String TABLE_MODE = "t_mode";
	public static final String TABLE_MODE_LIST = "t_mode_list";
	public static final String TABLE_DEVICE = "t_device";
	public static final String TABLE_DEVICE_ACTION = "t_device_action";
	public static final String TABLE_DEVICE_FUNCTION = "t_device_function";
	public static final String TABLE_FUNCTION_PARAM = "t_function_param";
	public static final String TABLE_ACTION_PARAM = "t_action_param";
	public static final String TABLE_DEVICE_TYPE = "t_device_type";
	public static final String TABLE_PRODUCTS = "t_products";
	public static final String TABLE_GATEWAY = "t_gateway";
	public static final String TABLE_GATEWAY_TYPE = "t_gateway_type";
	public static final String TABLE_CUS_DEVICE = "t_cusdevice";
	public static final String TABLE_INFRARED = "t_infrared";
	public static final String TABLE_VOICE = "t_voice";
	public static final String TABLE_SUBSCRIBE = "t_scribe";
	private SQLiteDatabase db = null;

	public WidgetDAOImpl(SQLiteDatabase db) {
		this.db = db;
	}

	public List<GateWay> getGateWay() {
		List<GateWay> list = new ArrayList<GateWay>();
		if(db!=null) {
			try {
				Cursor cursor = db.query(TABLE_GATEWAY, new String[]{"Id",
								"GatewayName", "GatewayID", "UserName", "UserPassWord",
								"TypeId", "IsCurrent", "State"}, null, null, null,
						null, "TypeId asc");
				int resultCount = cursor.getCount();
				if (resultCount == 0 || !cursor.moveToFirst()) {
					return list;
				}
				for (int i = 0; i < resultCount; i++) {
					GateWay gateWay = new GateWay();
					gateWay.setId(cursor.getInt(0));
					gateWay.setGatewayName(cursor.getString(1));
					gateWay.setGatewayID(cursor.getString(2));
					gateWay.setUserName(cursor.getString(3));
					gateWay.setUserPassWord(cursor.getString(4));
					gateWay.setTypeId(cursor.getInt(5));
					gateWay.setIsCurrent(cursor.getInt(6));
					gateWay.setGateWayPoint(cursor.getString(7));
					list.add(gateWay);
					cursor.moveToNext();
				}
			} catch (Exception e) {
			}
		}
		return list;
	}

	public List<GatewayType> getGateWayType() {
		List<GatewayType> list = new ArrayList<GatewayType>();
		Cursor cursor = db.query(TABLE_GATEWAY_TYPE, new String[] { "id",
				"gatewayTypeName", "gatewayTypeImage", "creationTime" }, null,
				null, null, null, "seqencing asc");
		int resultCount = cursor.getCount();
		try {

			if (resultCount == 0 || !cursor.moveToFirst()) {
				return list;
			}
			for (int i = 0; i < resultCount; i++) {
				GatewayType gateWayType = new GatewayType();
				gateWayType.setId(cursor.getInt(0));
				gateWayType.setGatewayTypeName(cursor.getString(1));
				list.add(gateWayType);
				cursor.moveToNext();
			}
		} catch (Exception e) {
		}
		return list;
	}

	public List<VoiceInfo> getVoice() {
		List<VoiceInfo> list = new ArrayList<VoiceInfo>();
		try {
			Cursor cursor = db.query(TABLE_VOICE, new String[] { "id",
					"voiceName", "voiceAnswer", "voiceAction", "userName" },
					null, null, null, null, null);
			int resultCount = cursor.getCount();
			if (resultCount == 0 || !cursor.moveToFirst()) {
				return list;
			}
			for (int i = 0; i < resultCount; i++) {
				VoiceInfo voiceInfo = new VoiceInfo();
				voiceInfo.setId(cursor.getInt(0));
				voiceInfo.setVoiceName(cursor.getString(1));
				voiceInfo.setVoiceAnswer(cursor.getString(2));
				voiceInfo.setVoiceAction(cursor.getString(3));
				voiceInfo.setUserName(cursor.getString(4));
				list.add(voiceInfo);
				cursor.moveToNext();
			}
		} catch (Exception e) {

		}
		return list;
	}

	public List<SubscribeInfo> getSubScribe() {
		List<SubscribeInfo> list = new ArrayList<SubscribeInfo>();
		try {
			Cursor cursor = db.query(TABLE_SUBSCRIBE, new String[] { "id",
					"subscribeName", "subscribeDate", "subscribeTime",
					"subscribeEvent", "subscribeAction", "userName" }, null,
					null, null, null, null);
			int resultCount = cursor.getCount();
			if (resultCount == 0 || !cursor.moveToFirst()) {
				return list;
			}
			for (int i = 0; i < resultCount; i++) {
				SubscribeInfo voiceInfo = new SubscribeInfo();
				voiceInfo.setId(cursor.getInt(0));
				voiceInfo.setSubscribeName(cursor.getString(1));
				voiceInfo.setSubscribeDate(cursor.getString(2));
				voiceInfo.setSubscribeTime(cursor.getString(3));
				voiceInfo.setSubscribeEvent(cursor.getString(4));
				voiceInfo.setSubscribeAction(cursor.getString(5));
				voiceInfo.setUserName(cursor.getString(6));
				list.add(voiceInfo);
				cursor.moveToNext();
			}
		} catch (Exception e) {
			String sql = "create table t_scribe(id INTEGER PRIMARY KEY AUTOINCREMENT,subscribeName varchar(100),subscribeDate varchar(25),subscribeTime varchar(25),subscribeEvent varchar(500),subscribeAction varchar(100),userName varchar(25))";
			db.execSQL(sql);
		}
		return list;
	}

	public List<DeviceFunction> getDeviceFunction() {
		List<DeviceFunction> list = new ArrayList<DeviceFunction>();

		try {
			Cursor cursor = db.query(TABLE_DEVICE_FUNCTION, new String[] {
					"id", "functionName", "functionLanguage", "typeId" }, null,
					null, null, null, null);
			int resultCount = cursor.getCount();
			if (resultCount == 0 || !cursor.moveToFirst()) {
				return list;
			}
			for (int i = 0; i < resultCount; i++) {
				DeviceFunction gateWayType = new DeviceFunction();
				gateWayType.setId(cursor.getInt(cursor.getColumnIndex("id")));
				gateWayType.setFunctionName(cursor.getString(cursor
						.getColumnIndex("functionName")));
				gateWayType.setFunctionLanguage(cursor.getString(cursor
						.getColumnIndex("functionLanguage")));
				gateWayType.setTypeId(cursor.getInt(cursor
						.getColumnIndex("typeId")));
				list.add(gateWayType);
				cursor.moveToNext();
			}
		} catch (Exception e) {

		}
		return list;
	}

	public List<ActionParam> getActionParam() {
		List<ActionParam> list = new ArrayList<ActionParam>();

		try {
			Cursor cursor = db.query(TABLE_ACTION_PARAM, new String[] { "id",
					"actionparam", "action_paramLanguage", "typeId" }, null,
					null, null, null, null);
			int resultCount = cursor.getCount();
			if (resultCount == 0 || !cursor.moveToFirst()) {
				return list;
			}
			for (int i = 0; i < resultCount; i++) {
				ActionParam gateWayType = new ActionParam();
				gateWayType.setId(cursor.getInt(cursor.getColumnIndex("id")));
				gateWayType.setActionparam(cursor.getString(cursor
						.getColumnIndex("actionparam")));
				gateWayType.setAction_paramLanguage(cursor.getString(cursor
						.getColumnIndex("action_paramLanguage")));
				gateWayType.setTypeId(cursor.getInt(cursor
						.getColumnIndex("typeId")));
				list.add(gateWayType);
				cursor.moveToNext();
			}
		} catch (Exception e) {

		}
		return list;
	}

	public List<FunctionParam> getFunctionParam() {
		List<FunctionParam> list = new ArrayList<FunctionParam>();

		try {
			Cursor cursor = db.query(TABLE_FUNCTION_PARAM, new String[] { "id",
					"function_param", "function_paramLanguage", "typeId" },
					null, null, null, null, null);
			int resultCount = cursor.getCount();
			if (resultCount == 0 || !cursor.moveToFirst()) {
				return list;
			}
			for (int i = 0; i < resultCount; i++) {
				FunctionParam gateWayType = new FunctionParam();
				gateWayType.setId(cursor.getInt(cursor.getColumnIndex("id")));
				gateWayType.setFunction_param(cursor.getString(cursor
						.getColumnIndex("function_param")));

				gateWayType.setFunction_paramLanguage(cursor.getString(cursor
						.getColumnIndex("function_paramLanguage")));
				gateWayType.setTypeId(cursor.getInt(cursor
						.getColumnIndex("typeId")));
				list.add(gateWayType);
				cursor.moveToNext();
			}
		} catch (Exception e) {

		}
		return list;
	}

	public List<DeviceAction> getDeviceAction() {
		List<DeviceAction> list = new ArrayList<DeviceAction>();

		try {
			Cursor cursor = db.query(TABLE_DEVICE_ACTION, new String[] { "id",
					"actionName", "actionLanguage", "typeId" }, null, null,
					null, null, null);
			int resultCount = cursor.getCount();
			if (resultCount == 0 || !cursor.moveToFirst()) {
				return list;
			}
			for (int i = 0; i < resultCount; i++) {
				DeviceAction gateWayType = new DeviceAction();
				gateWayType.setId(cursor.getInt(cursor.getColumnIndex("id")));
				gateWayType.setActionName(cursor.getString(cursor
						.getColumnIndex("actionName")));
				gateWayType.setActionLanguage(cursor.getString(cursor
						.getColumnIndex("actionLanguage")));
				gateWayType.setTypeId(cursor.getInt(cursor
						.getColumnIndex("typeId")));
				list.add(gateWayType);
				cursor.moveToNext();
			}
		} catch (Exception e) {

		}
		return list;
	}

    public List<Mode> getMode() {
        List<Mode> list = new ArrayList<Mode>();
        if(db!=null) {
            try {
                Cursor cursor = db.query(TABLE_MODE, new String[]{"Id", "ModeName",
                                "ModeImage", "ModeType", "ModeCode", "ModeLoop",
                                "StartTime", "EndTime","ModeNickName","IsShow", "Seqencing","GatewayID"}, null, null, null, null,
                        "Seqencing asc");
                int resultCount = cursor.getCount();
                if (resultCount == 0 || !cursor.moveToFirst()) {
                    return list;
                }
                for (int i = 0; i < resultCount; i++) {
                    Mode mode = new Mode();
                    mode.setId(cursor.getInt(cursor.getColumnIndex("Id")));
                    mode.setModeName(cursor.getString(cursor
                            .getColumnIndex("ModeName")));
                    mode.setModeImage(cursor
                            .getString(cursor.getColumnIndex("ModeImage")));
                    mode.setModeType(cursor.getString(cursor
                            .getColumnIndex("ModeType")));
                    mode.setModeCode(cursor.getInt(cursor
                            .getColumnIndex("ModeCode")));
                    mode.setSeqencing(cursor.getInt(cursor
                            .getColumnIndex("Seqencing")));
                    mode.setModeLoop(cursor.getInt(cursor
                            .getColumnIndex("ModeLoop")));
                    mode.setStartTime(cursor.getString(cursor
                            .getColumnIndex("StartTime")));
                    mode.setEndTime(cursor.getString(cursor
                            .getColumnIndex("EndTime")));
                    mode.setModeNickName(cursor.getString(cursor
                            .getColumnIndex("ModeNickName")));
                    mode.setCreationTime("");
					mode.setGatewayID(cursor.getString(cursor
							.getColumnIndex("GatewayID")));
					mode.setIsShow(cursor.getInt(cursor
							.getColumnIndex("IsShow")));
                    list.add(mode);
                    cursor.moveToNext();
                }
            } catch (Exception e) {

            }
        }
        return list;
    }

	public List<ModeList> getModeList() {
		List<ModeList> list = new ArrayList<ModeList>();
		try{
			String sql = "select *,l.Id as m_id,l.CreationTime as mouth,d.DeviceImage,DT.DeviceTypeName,DT.DeviceTypeKey,r.RoomName from t_mode_list l left join t_device d on d.Id=l.DeviceId left join t_room r on r.Id=d.RoomId left join t_products p on d.ProductsKey=p.ProductsKey LEFT JOIN t_device_type dt on d.DeviceTypeKey=dt.DeviceTypeKey where d.Id=l.DeviceId order by l.Seqencing";
			if(db!=null) {
				Cursor cursor = db.rawQuery(sql, null);
				if (cursor != null) {
					int resultCount = cursor.getCount();

					if (resultCount == 0 || !cursor.moveToFirst()) {
						return list;
					}
					for (int i = 0; i < resultCount; i++) {
						ModeList modeList = new ModeList();
						modeList.setId(cursor.getInt(cursor.getColumnIndex("m_id")));

						String ModeAction = cursor.getString(cursor
								.getColumnIndex("ModeAction"));
						String ModeFunction = cursor.getString(cursor
								.getColumnIndex("ModeFunction"));
						String ModeTime = cursor.getString(cursor
								.getColumnIndex("ModeTime"));
						String ModeDelayed = cursor.getString(cursor
								.getColumnIndex("ModeDelayed"));
						String ModePeriod = cursor.getString(cursor
								.getColumnIndex("ModePeriod"));
						modeList.setModeAction(ModeAction);
						modeList.setModeFunction(ModeFunction);
						modeList.setModeTime(ModeTime);
						modeList.setModeDelayed(ModeDelayed);
						modeList.setModePeriod(ModePeriod);

						modeList.setModeId(cursor.getInt(cursor
								.getColumnIndex("ModeId")));
						modeList.setDeviceId(cursor.getInt(cursor
								.getColumnIndex("DeviceId")));

						modeList.setDeviceNickName(cursor.getString(cursor
								.getColumnIndex("DeviceNickName")));
						modeList.setRoomName(cursor.getString(cursor
								.getColumnIndex("RoomName")));
						modeList.setDeviceTypeKey(cursor.getInt(cursor
								.getColumnIndex("DeviceTypeKey")));
						modeList.setDeviceTypeClick(cursor.getInt(cursor
								.getColumnIndex("DeviceTypeClick")));

						modeList.setSeqencing(cursor.getInt(cursor
								.getColumnIndex("Seqencing")));
						modeList.setDeviceOnLine(cursor.getInt(cursor
								.getColumnIndex("DeviceOnLine")));
						modeList.setDeviceTimer(cursor.getString(cursor
								.getColumnIndex("DeviceTimer")));
						modeList.setDeviceOrdered(cursor.getString(cursor
								.getColumnIndex("DeviceOrdered")));
						modeList.setDeviceName(cursor.getString(cursor
								.getColumnIndex("DeviceName")));
						modeList.setDeviceID(cursor.getString(cursor
								.getColumnIndex("DeviceID")));

						String deviceImage=cursor.getString(cursor
								.getColumnIndex("DeviceImage"));
						if(deviceImage==null||deviceImage==""||deviceImage.equals("")){
							deviceImage=cursor.getString(cursor
									.getColumnIndex("ProductsImage"));
							modeList.setDeviceImage(deviceImage);
						}else{
							modeList.setDeviceImage(deviceImage);
						}
						modeList.setDeviceIO(cursor.getString(cursor
								.getColumnIndex("DeviceIO")));
						modeList.setDeviceTypeName(cursor.getString(cursor
								.getColumnIndex("DeviceTypeName")));
						modeList.setProductsCode(cursor.getString(cursor
								.getColumnIndex("ProductsCode")));
						modeList.setStartTime(cursor.getString(cursor
								.getColumnIndex("StartTime")));
						modeList.setEndTime(cursor.getString(cursor
								.getColumnIndex("EndTime")));
						modeList.setDeviceTypeKey(cursor.getInt(cursor
								.getColumnIndex("DeviceTypeKey")));
						modeList.setProductsKey(cursor.getInt(cursor
								.getColumnIndex("ProductsKey")));
						modeList.setProductsIO(cursor.getInt(cursor
								.getColumnIndex("ProductsIO")));
						modeList.setBeginMonth(cursor.getString(cursor
								.getColumnIndex("BeginMonth")));
						modeList.setEndMonth(cursor.getString(cursor
								.getColumnIndex("EndMonth")));
						list.add(modeList);
						cursor.moveToNext();
					}
				}
			}
		}catch (Exception e){

		}
		return list;
	}

    public List<Products> getProducts() {
		List<Products> list = new ArrayList<Products>();
		try {
			Cursor cursor = db.query(TABLE_PRODUCTS, new String[] { "id",
					"products_name", "products_code", "products_version",
					"products_IO", "products_Id" }, null, null, null, null,
					null);
			int resultCount = cursor.getCount();
			if (resultCount == 0 || !cursor.moveToFirst()) {
				return list;
			}
			for (int i = 0; i < resultCount; i++) {
				Products products = new Products();
				products.setId(cursor.getInt(cursor.getColumnIndex("id")));
				products.setProducts_name(cursor.getString(cursor
						.getColumnIndex("products_name")));
				products.setProducts_code(cursor.getString(cursor
						.getColumnIndex("products_code")));
				products.setProducts_version(cursor.getInt(cursor
						.getColumnIndex("products_version")));
				products.setProducts_IO(cursor.getInt(cursor
						.getColumnIndex("products_IO")));
				products.setProducts_Id(cursor.getInt(cursor
						.getColumnIndex("products_Id")));
				list.add(products);
				cursor.moveToNext();
			}
		} catch (Exception e) {

		}
		return list;
	}
	public int insertMode(Mode mode) {
		int num = -1;
		try {
            ContentValues contentValues = new ContentValues();
            contentValues.put("ModeName", mode.getModeName());
            contentValues.put("ModeImage",mode.getModeImage());
            contentValues.put("ModeType", mode.getModeType());
            contentValues.put("ModeCode", mode.getModeCode());
            contentValues.put("Seqencing", mode.getSeqencing());
            contentValues.put("CreationTime", getSysNowTime());
            contentValues.put("StartTime", mode.getStartTime());
            contentValues.put("EndTime", mode.getEndTime());
            contentValues.put("ModeLoop", mode.getModeLoop());
            contentValues.put("ModeNickName", mode.getModeNickName());
            contentValues.put("GatewayID", mode.getGatewayID());
            contentValues.put("IsShow",mode.getIsShow());
            Cursor cursor = db.query(TABLE_MODE, new String[] { "Id" }, "Id=?",
					new String[] { String.valueOf(mode.getId()) }, null, null,
					null, null);
			int resultNumber = cursor.getCount();
			if (resultNumber > 0) {

				db.update(TABLE_MODE, contentValues, "Id=?",
						new String[] { String.valueOf(mode.getId()) });
				num = 2;
			} else {
                contentValues.put("Id", mode.getId());
				db.insert(TABLE_MODE, null, contentValues);
				num = 1;
			}
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println(e.toString());
			num = -1;
		}
		return num;
	}

	public int insertVoice(VoiceInfo voiceInfo) {
		int num = -1;
		try {
			ContentValues contentValues = new ContentValues();
			contentValues.put("voiceName", voiceInfo.getVoiceName());
			contentValues.put("voiceAnswer", voiceInfo.getVoiceAnswer());
			contentValues.put("voiceAction", voiceInfo.getVoiceAction());
			contentValues.put("userName", voiceInfo.getUserName());
			db.insert(TABLE_VOICE, null, contentValues);
			num = 1;
		} catch (Exception e) {
			num = -1;
		}
		return num;
	}

	public int updateMode(Mode mode, String modeName) {
		int num = 0;
        ContentValues contentValues = new ContentValues();
        contentValues.put("ModeName", mode.getModeName());
        contentValues.put("ModeImage",mode.getModeImage());
        contentValues.put("ModeType", mode.getModeType());
        contentValues.put("ModeCode", mode.getModeCode());
        contentValues.put("Seqencing", mode.getSeqencing());
        contentValues.put("CreationTime", getSysNowTime());
        contentValues.put("StartTime", mode.getStartTime());
        contentValues.put("EndTime", mode.getEndTime());
        contentValues.put("ModeLoop", mode.getModeLoop());
        contentValues.put("ModeNickName", mode.getModeNickName());
        contentValues.put("GatewayID", mode.getGatewayID());
		contentValues.put("IsShow",mode.getIsShow());
		try {

			num = db.update(TABLE_MODE, contentValues, "ModeName=?",
					new String[] { modeName });

		} catch (Exception e) {
			// TODO: handle exception
			System.out.println(e.toString());
			num = -1;
		}
		return num;
	}

	public int updateDevice(Device device, String oldName) {
		int num = 0;
        ContentValues contentValues = new ContentValues();
        contentValues.put("DeviceName", device.getDeviceName());
        contentValues.put("deviceImage", device.getDeviceImage());
        contentValues.put("DeviceID", device.getDeviceID());
        contentValues.put("deviceNickName", device.getDeviceNickName());
        contentValues.put("CreationTime", getSysNowTime());
        contentValues.put("Seqencing", device.getSeqencing());
        contentValues.put("DeviceOnLine", device.getDeviceOnLine());
        contentValues.put("DeviceTimer", device.getDeviceTimer());
        contentValues.put("DeviceIO", device.getDeviceIO());
        contentValues.put("DeviceOrdered", device.getDeviceOrdered());
        contentValues.put("StartTime", device.getStartTime());
        contentValues.put("EndTime", device.getEndTime());
        contentValues.put("sceneId", device.getSceneId());
        contentValues.put("DeviceTypeKey", device.getDeviceTypeKey());
        contentValues.put("ProductsKey", device.getProductsKey());
        contentValues.put("GradingId", device.getGradingId());
        contentValues.put("RoomId", device.getRoomId());

		try {
			num = db.update(TABLE_DEVICE, contentValues, "DeviceName=?",
					new String[] { oldName });

		} catch (Exception e) {
			// TODO: handle exception
			System.out.println(e.toString());
			num = -1;
		}
		return num;
	}

	public int insertGateWay(GateWay gateWay) {

		int num = 0;
		try {
			Cursor result = db.query(TABLE_GATEWAY,
					new String[] { "GatewayName" }, "GatewayName=?",
					new String[] { gateWay.getGatewayName() }, null, null,
					null, null);

			if (result.getCount() > 0) {
				num = 1;
			} else {
				ContentValues contentValues = new ContentValues();
                contentValues.put("GatewayName", gateWay.getGatewayName());
                contentValues.put("GatewayID", gateWay.getGatewayID());
                contentValues.put("UserName", gateWay.getUserName());
                contentValues.put("UserPassWord", gateWay.getUserPassWord());
                contentValues.put("TypeId", gateWay.getTypeId());
                contentValues.put("CreationTime", getSysNowTime());
                contentValues.put("IsCurrent", gateWay.getIsCurrent());
                contentValues.put("State", gateWay.getState());
                contentValues.put("Seqencing", gateWay.getSeqencing());
				num += db.insert(TABLE_GATEWAY, null, contentValues);
			}
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println(e.toString());
			num = -1;
		}
		return num;
	}

	@Override
	public List<RedInfra> getInfrareds() {
		// TODO Auto-generated method stub
		List<RedInfra> list = new ArrayList<RedInfra>();
		try {
			String sql = "select *,l.Id as lid,l.Seqencing as lseqencing from t_infrared l left join t_device d on d.Id=l.FatherId where l.FatherId=d.Id order by l.Seqencing";
			Cursor cursor = db.rawQuery(sql, null);
			int resultCount = cursor.getCount();
			if (resultCount == 0 || !cursor.moveToFirst()) {
				return list;
			}
			for (int i = 0; i < resultCount; i++) {
				RedInfra infrared = new RedInfra();
				infrared.setId(cursor.getInt(cursor.getColumnIndex("lid")));
				infrared.setInfraredName(cursor.getString(cursor
						.getColumnIndex("InfraredName")));
				infrared.setInfraredImage(cursor.getString(cursor
						.getColumnIndex("InfraredImage")));
				infrared.setDeviceId(cursor.getString(cursor
						.getColumnIndex("DeviceId")));
				infrared.setFatherId(cursor.getInt(cursor
						.getColumnIndex("FatherId")));
				infrared.setInfraredIndex(cursor.getInt(cursor
						.getColumnIndex("InfraredIndex")));
				infrared.setInfraredBrandId(cursor.getInt(cursor
						.getColumnIndex("InfraredBrandId")));
				infrared.setInfraredBrandName(cursor.getString(cursor
						.getColumnIndex("InfraredBrandName")));
				infrared.setInfraredVersionId(cursor.getInt(cursor
						.getColumnIndex("InfraredVersionId")));
				infrared.setInfraredVersionName(cursor.getString(cursor
						.getColumnIndex("InfraredVersionName")));
				infrared.setInfraredKey(cursor.getInt(cursor
						.getColumnIndex("InfraredKey")));
				infrared.setInfraredButtonId(cursor.getInt(cursor
						.getColumnIndex("InfraredButtonId")));
				infrared.setInfraredCode(cursor.getString(cursor
						.getColumnIndex("InfraredCode")));
				infrared.setInfraredStudyType(cursor.getInt(cursor
						.getColumnIndex("InfraredStudyType")));
				infrared.setInfraredX(cursor.getInt(cursor
						.getColumnIndex("InfraredX")));
				infrared.setInfraredY(cursor.getInt(cursor
						.getColumnIndex("InfraredY")));
				infrared.setCreationTime(cursor.getString(cursor
						.getColumnIndex("CreationTime")));
				infrared.setSeqencing(cursor.getInt(cursor
						.getColumnIndex("lseqencing")));
				list.add(infrared);
				cursor.moveToNext();
			}
		} catch (Exception e) {
		}
		return list;
	}

	public int insertModeList(ModeList modeList) {
		int num = 0;
		try {
			ContentValues contentValues = new ContentValues();
            contentValues.put("ModeAction", modeList.getModeAction());
            contentValues.put("ModeFunction",modeList.getModeFunction());
            contentValues.put("ModeTime", modeList.getModeTime());
            contentValues.put("ModeDelayed", modeList.getModeDelayed());
            contentValues.put("ModePeriod", modeList.getModePeriod());
            contentValues.put("CreationTime", getSysNowTime());
            contentValues.put("DeviceId", modeList.getDeviceId());
            contentValues.put("Seqencing", modeList.getSeqencing());
            contentValues.put("ModeId", modeList.getModeId());
            contentValues.put("BeginMonth", modeList.getBeginMonth());
            contentValues.put("EndMonth", modeList.getEndMonth());
            contentValues.put("LanguageId", 1);
            contentValues.put("CompanyId", 1);
            contentValues.put("Data1","0");
            contentValues.put("Data2", "0");
			Cursor cursor = db.query(TABLE_MODE_LIST, new String[] { "Id" },
					"Id=?", new String[] { String.valueOf(modeList.getId()) },
					null, null, null, null);
			int resultNumber = cursor.getCount();
			if (resultNumber > 0) {
				db.update(TABLE_MODE_LIST, contentValues, "Id=?",
						new String[] { String.valueOf(modeList.getId()) });
				num = 2;
			} else {
				db.insert(TABLE_MODE_LIST, null, contentValues);
				num = 1;
			}
		} catch (Exception e) {
			num = -1;
		}
		return num;
	}

	public int updateModeList(ModeList modeList) {
		int num = 0;
		try {
			ContentValues contentValues = new ContentValues();
            contentValues.put("ModeAction", modeList.getModeAction());
            contentValues.put("ModeFunction",modeList.getModeFunction());
            contentValues.put("ModeTime", modeList.getModeTime());
            contentValues.put("ModeDelayed", modeList.getModeDelayed());
            contentValues.put("ModePeriod", modeList.getModePeriod());
            contentValues.put("CreationTime",modeList.getCreationTime());
            contentValues.put("DeviceId", modeList.getDeviceId());
            contentValues.put("Seqencing", modeList.getSeqencing());
            contentValues.put("ModeId", modeList.getModeId());
            contentValues.put("BeginMonth", modeList.getBeginMonth());
            contentValues.put("EndMonth", modeList.getEndMonth());
            contentValues.put("CreationTime",getSysNowTime());
            contentValues.put("LanguageId", 1);
            contentValues.put("CompanyId", 1);
            contentValues.put("Data1","0");
            contentValues.put("Data2", "0");
			num = db.update(TABLE_MODE_LIST, contentValues, "Id=?",
					new String[] { String.valueOf(modeList.getId()) });
		} catch (Exception e) {
			num = -1;
		}
		return num;
	}

	public int insertDevice(Device device) {
		int num = -1;
		try {
			ContentValues contentValues = new ContentValues();
			contentValues.put("DeviceName", device.getDeviceName());
			contentValues.put("DeviceImage",device.getDeviceImage());
			contentValues.put("DeviceID", device.getDeviceID());
			contentValues.put("deviceNickName", device.getDeviceNickName());
			contentValues.put("CreationTime", getSysNowTime());
			contentValues.put("Seqencing", device.getSeqencing());
			contentValues.put("DeviceOnLine", device.getDeviceOnLine());
			contentValues.put("DeviceTimer", device.getDeviceTimer());
			contentValues.put("DeviceIO", device.getDeviceIO());
			contentValues.put("DeviceOrdered", device.getDeviceOrdered());
			contentValues.put("StartTime", device.getStartTime());
			contentValues.put("EndTime", device.getEndTime());
			contentValues.put("sceneId", device.getSceneId());
			contentValues.put("DeviceTypeKey", device.getDeviceTypeKey());
			contentValues.put("ProductsKey", device.getProductsKey());
			contentValues.put("GradingId", device.getGradingId());
			contentValues.put("RoomId", device.getRoomId());
			Cursor cursor = db.query(TABLE_DEVICE, new String[] { "Id" },
					"Id=?", new String[] { String.valueOf(device.getId()) },
					null, null, null, null);
			int resultNumber = cursor.getCount();

			String name = DeviceFactory.judgeName(device.getRoomId(), device.getDeviceName()
					.toString());
			if (!name.equals("")
					&& (name.indexOf(device.getDeviceName()) != -1 || device
					.getDeviceName().indexOf(name) != -1)) {

				if (device.getId() != 0) {
					contentValues.put("Id", device.getId());
				}
				db.update(TABLE_DEVICE, contentValues, "DeviceName=? and RoomId=?",
						new String[] { name,String.valueOf(device.getRoomId()) });
				num = 2;
			} else {
				if (resultNumber == 0 && device.getId() != 0) {
					contentValues.put("Id", device.getId());
				}
				db.insert(TABLE_DEVICE, null, contentValues);
				num = 1;

			}

		} catch (Exception e) {
			// TODO: handle exception
			System.out.println(e.toString());
			num = -1;
		}
		return num;
	}


	public int insertAction(DeviceAction deviceAction) {
		try {
			ContentValues contentValues = new ContentValues();
			contentValues.put("actionName", deviceAction.getActionName());
			contentValues.put("actionLanguage",
					deviceAction.getActionLanguage());
			contentValues.put("typeId", deviceAction.getTypeId());

			db.insert(TABLE_DEVICE_ACTION, null, contentValues);
		} catch (Exception e) {
			// TODO: handle exception
		}
		return 0;
	}

	public int insetFunction(DeviceFunction deviceFunction) {
		try {
			try {
				ContentValues contentValues = new ContentValues();
				contentValues.put("functionName",
						deviceFunction.getFunctionName());
				contentValues.put("functionLanguage",
						deviceFunction.getFunctionLanguage());
				contentValues.put("typeId", deviceFunction.getTypeId());

				db.insert(TABLE_DEVICE_FUNCTION, null, contentValues);
			} catch (Exception e) {
				// TODO: handle exception
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		return 0;
	}

	public int updateGateWay(GateWay gateWay, String gateWayName) {
		int num = 0;
		ContentValues contentValues = new ContentValues();
		contentValues.put("GatewayName", gateWay.getGatewayName());
		contentValues.put("GatewayID", gateWay.getGatewayID());
		contentValues.put("UserName", gateWay.getUserName());
		contentValues.put("UserPassWord", gateWay.getUserPassWord());
		contentValues.put("TypeId", gateWay.getTypeId());
		contentValues.put("CreationTime", getSysNowTime());
		contentValues.put("IsCurrent", gateWay.getIsCurrent());
		contentValues.put("State", gateWay.getState());
		contentValues.put("seqencing", gateWay.getSeqencing());

		try {
			num = db.update(TABLE_GATEWAY, contentValues, "Id=?",
					new String[] { String.valueOf(gateWay.getId()) });
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println(e.toString());
			num = -1;
		}
		return num;
	}

	public int updateIsCurrent(int id, int current) {

		int resultCount = 0;
		try {

			db.execSQL("update t_gateway set isCurrent=0 where isCurrent=1");
			db.execSQL("update t_gateway set isCurrent=? where id=?",
					new Object[] { current, id });
			resultCount = 1;
		} catch (Exception e) {
			// TODO: handle exception
			resultCount = 0;
		}

		return resultCount;
	}

	@Override
	public int deleteGateWay(int Id) {
		// TODO Auto-generated method stub
		int resultCount = 0;
		try {
			resultCount = db.delete(TABLE_GATEWAY, "id=?",
					new String[] { String.valueOf(Id) });

		} catch (Exception e) {
			// TODO: handle exception
			resultCount = -1;
		}
		return resultCount;
	}

	public int deleteVoice(int Id) {
		int resultCount = 0;
		try {
			resultCount = db.delete(TABLE_VOICE, "id=?",
					new String[] { String.valueOf(Id) });

		} catch (Exception e) {
			// TODO: handle exception
			resultCount = -1;
		}
		return resultCount;
	}

    public List<Room> getRoom() {
        List<Room> list = new ArrayList<Room>();

        if(db!=null){
            Cursor cursor = db.query(TABLE_ROOM, new String[] { "Id", "RoomName",
                            "RoomImage", "IsShow", "CreationTime", "Seqencing" },
                    null, null, null, null, "Seqencing asc");

            int resultCount = cursor.getCount();
            try {

                if (resultCount == 0 || !cursor.moveToFirst()) {
                    return list;
                }
                for (int i = 0; i < resultCount; i++) {
                    Room room = new Room();
                    room.setId(cursor.getInt(0));
                    room.setRoomName(cursor.getString(1));
                    room.setRoomImage(cursor.getString(2));

                    room.setIsShow(cursor.getInt(3));
                    room.setCreationTime(cursor.getString(4));
                    room.setSeqencing(cursor.getInt(5));
                    list.add(room);
                    cursor.moveToNext();
                }
            } catch (Exception e) {
            }
        }
        return list;
    }


	public int insertRoom(Room room) {
		int num = 0;
		try {
			ContentValues contentValues = new ContentValues();
            contentValues.put("RoomName",room.getRoomName());
            contentValues.put("RoomImage",room.getRoomImage());
            contentValues.put("CreationTime",getSysNowTime());
            contentValues.put("IsShow",room.getIsShow());
            contentValues.put("Seqencing", room.getSeqencing());
			Cursor cursor = db.query(TABLE_ROOM, new String[] { "Id" },
					"Id=?", new String[] { String.valueOf(room.getId()) },
					null, null, null, null);
			int resultNumber = cursor.getCount();
			if(resultNumber>0){
				num +=db.update(TABLE_ROOM,contentValues, "Id=?",
						new String[] { String.valueOf(room.getId())});
			}else {
                contentValues.put("Id", room.getId());
				num += db.insert(TABLE_ROOM, null, contentValues);
			}
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println(e.toString());
			num = -1;
		}
		return num;
	}

	public int updateVoice(VoiceInfo voiceInfo) {
		int num = 0;
		ContentValues contentValues = new ContentValues();
		contentValues.put("voiceName", voiceInfo.getVoiceName());
		contentValues.put("voiceAnswer", voiceInfo.getVoiceAnswer());
		contentValues.put("voiceAction", voiceInfo.getVoiceAction());
		contentValues.put("userName", voiceInfo.getUserName());

		try {
			num = db.update(TABLE_VOICE, contentValues, "id=?",
					new String[] { String.valueOf(voiceInfo.getId()) });

		} catch (Exception e) {
			// TODO: handle exception
			System.out.println(e.toString());
			num = -1;
		}
		return num;

	}

	public int updateRoom(Room room, String roomName) {
		int num = 0;
		ContentValues contentValues = new ContentValues();
		contentValues.put("RoomName", room.getRoomName());
		contentValues.put("RoomImage",room.getRoomImage());
		contentValues.put("CreationTime", getSysNowTime());
		contentValues.put("IsShow", room.getIsShow());
		contentValues.put("Seqencing", room.getSeqencing());

		try {
			Cursor cursor = db.query(TABLE_ROOM, new String[] { "Id" },
					"RoomName=?", new String[] { room.getRoomName() }, null,
					null, null, null);

			if (cursor.getCount() > 1) {
				num = 2;
			} else if (cursor.getCount() == 1) {
				cursor.moveToLast();
				int Id = cursor.getInt(cursor.getColumnIndex("Id"));
				cursor.close();

				if (Id == room.getId()) {
					num = db.update(TABLE_ROOM, contentValues, "Id=?",
							new String[] { String.valueOf(room.getId()) });
				} else {
					num = 2;
				}
			} else {
				num = db.update(TABLE_ROOM, contentValues, "Id=?",
						new String[] { String.valueOf(room.getId()) });
			}
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println(e.toString());
			num = -1;
		}
		return num;
	}

    @Override
    public List<Device> getDevices() {
        // TODO Auto-generated method stub
        List<Device> list = new ArrayList<Device>();
        try {
            String sql = "select D.*,D.Seqencing, a.RoomName, p.ProductsName,p.ProductsCode,p.IsSwitch,p.ProductsKey,p.ProductsIO,p.ActionShow,p.FunctionShow,p.ActionPramShow,p.FunctionParamShow,DT.DeviceTypeKey,DT.DeviceTypeClick,DT.DeviceTypeName FROM t_device D LEFT JOIN t_room a on d.RoomId=a.Id left JOIN t_products p on d.ProductsKey=p.ProductsKey LEFT JOIN t_device_type dt on d.DeviceTypeKey=dt.DeviceTypeKey order by dt.Seqencing,d.Seqencing asc";
            if (db != null) {
                Cursor cursor = db.rawQuery(sql, null);
                if (cursor != null) {
                    int resultCount = cursor.getCount();
                    if (resultCount == 0 || !cursor.moveToFirst()) {
                        return list;
                    }
                    for (int i = 0; i < resultCount; i++) {
                        Device device = new Device();
                        device.setId(cursor.getInt(cursor.getColumnIndex("Id")));
                        device.setDeviceTypeClick(cursor.getInt(cursor
                                .getColumnIndex("DeviceTypeClick")));
                        device.setDeviceName(cursor.getString(cursor
                                .getColumnIndex("DeviceName")));
                        device.setDeviceTypeName(cursor.getString(cursor
                                .getColumnIndex("DeviceTypeName")));
                        device.setProductsName(cursor.getString(cursor
                                .getColumnIndex("ProductsName")));
                        device.setProductsCode(cursor.getString(cursor
                                .getColumnIndex("ProductsCode")));
                        device.setDeviceID(cursor.getString(cursor
                                .getColumnIndex("DeviceID")));
                        device.setDeviceImage(cursor.getString(cursor
                                .getColumnIndex("DeviceImage")));
                        device.setDeviceTypeKey(cursor.getInt(cursor
                                .getColumnIndex("DeviceTypeKey")));
                        device.setDeviceIO(cursor.getString(cursor
                                .getColumnIndex("DeviceIO")));
                        device.setDeviceNickName(cursor.getString(cursor
                                .getColumnIndex("DeviceNickName")));
                        device.setSeqencing(cursor.getInt(cursor
                                .getColumnIndex("Seqencing")));
                        device.setDeviceOnLine(cursor.getInt(cursor
                                .getColumnIndex("DeviceOnLine")));
                        device.setDeviceTimer(cursor.getString(cursor
                                .getColumnIndex("DeviceTimer")));
                        device.setDeviceOrdered(cursor.getString(cursor
                                .getColumnIndex("DeviceOrdered")));
                        device.setStartTime(cursor.getString(cursor
                                .getColumnIndex("StartTime")));
                        device.setEndTime(cursor.getString(cursor
                                .getColumnIndex("EndTime")));
                        device.setSceneId(cursor.getInt(cursor
                                .getColumnIndex("SceneId")));
                        device.setRoomName(cursor.getString(cursor
                                .getColumnIndex("RoomName")));
                        device.setCreationTime(cursor.getString(cursor
                                .getColumnIndex("CreationTime")));
                        device.setGradingId(cursor.getInt(cursor
                                .getColumnIndex("GradingId")));
                        device.setRoomId(cursor.getInt(cursor
                                .getColumnIndex("RoomId")));
                        device.setDeviceTypeKey(cursor.getInt(cursor
                                .getColumnIndex("DeviceTypeKey")));
                        device.setIsSwitch(cursor.getInt(cursor
                                .getColumnIndex("IsSwitch")));
                        device.setProductsKey(cursor.getInt(cursor
                                .getColumnIndex("ProductsKey")));
                        device.setProductsIO(cursor.getInt(cursor
                                .getColumnIndex("ProductsIO")));
                        device.setActionShow(cursor.getInt(cursor
                                .getColumnIndex("ActionShow")));
                        device.setFunctionShow(cursor.getInt(cursor
                                .getColumnIndex("FunctionShow")));
                        device.setActionPramShow(cursor.getInt(cursor
                                .getColumnIndex("ActionPramShow")));
                        device.setFunctionParamShow(cursor.getInt(cursor
                                .getColumnIndex("FunctionParamShow")));
                        list.add(device);
                        cursor.moveToNext();
                    }
                }
            }
        }catch (Exception e){

        }
        return list;
    }

	@Override
	public List<CusDevice> getCusDevices() {
		// TODO Auto-generated method stub
		List<CusDevice> list = new ArrayList<CusDevice>();
		try {
			Cursor cursor = db.query(TABLE_CUS_DEVICE, new String[] { "id",
					"deviceName", "deviceDes", "deviceType", "deviceDid",
					"deviceIo", "deviceTypeId", "deviceProductId" }, null,
					null, null, null, null);
			if (cursor != null) {
				int resultCount = cursor.getCount();
				if (resultCount == 0 || !cursor.moveToFirst()) {
					return list;
				}
				for (int i = 0; i < resultCount; i++) {
					CusDevice device = new CusDevice();
					device.setId(cursor.getInt(cursor.getColumnIndex("id")));
					device.setDeviceName(cursor.getString(cursor
							.getColumnIndex("deviceName")));
					device.setDeviceDes(cursor.getString(cursor
							.getColumnIndex("deviceDes")));
					device.setDeviceType(cursor.getString(cursor
							.getColumnIndex("deviceType")));
					device.setDeviceDid(cursor.getString(cursor
							.getColumnIndex("deviceDid")));
					device.setDeviceIo(cursor.getString(cursor
							.getColumnIndex("deviceIo")));
					device.setDeviceProductId(cursor.getInt(cursor
							.getColumnIndex("deviceProductId")));
					device.setDeviceTypeId(cursor.getInt(cursor
							.getColumnIndex("deviceTypeId")));
					list.add(device);
					cursor.moveToNext();
				}
			}
		} catch (Exception e) {
		}
		return list;
	}

	@Override
	public int insertInfrad(RedInfra infrared) {
		// TODO Auto-generated method stub

		int num = 0;
		try {
			ContentValues contentValues = new ContentValues();
			if (infrared.getId() != 0) {
				contentValues.put("Id", infrared.getId());
			}
			contentValues.put("InfraredName", infrared.getInfraredName());
			contentValues.put("InfraredImage", infrared.getInfraredImage());
			contentValues.put("DeviceId", infrared.getDeviceId());
			contentValues.put("FatherId", infrared.getFatherId());
			contentValues.put("InfraredIndex", infrared.getInfraredIndex());
			contentValues.put("InfraredBrandId", infrared.getInfraredBrandId());
			contentValues.put("InfraredBrandName", infrared.getInfraredBrandName());
			contentValues.put("InfraredVersionId",
					infrared.getInfraredVersionId());
			contentValues.put("InfraredVersionName",
					infrared.getInfraredVersionName());
			contentValues.put("InfraredKey",
					infrared.getInfraredKey());
			contentValues.put("InfraredButtonId", infrared.getInfraredButtonId());
			contentValues.put("InfraredCode", infrared.getInfraredCode());
			contentValues.put("InfraredStudyType", infrared.getInfraredStudyType());
			contentValues.put("InfraredX", infrared.getInfraredX());
			contentValues.put("InfraredY", infrared.getInfraredY());
			contentValues.put("CreationTime", getSysNowTime());
			contentValues.put("Seqencing", infrared.getSeqencing());

			Cursor cursor = db.query(TABLE_INFRARED, new String[] { "Id" },
					"Id=?", new String[] { String.valueOf(infrared.getId()) },
					null, null, null, null);
			int resultNumber = cursor.getCount();
			if (resultNumber > 0) {

				db.update(TABLE_INFRARED, contentValues, "Id=?",
						new String[] { String.valueOf(infrared.getId()) });
				num = 2;
			} else {
				db.insert(TABLE_INFRARED, null, contentValues);
				num = 1;
			}
		} catch (Exception e) {
			// TODO: handle exception
			num = -1;
		}
		return num;
	}


    @Override
    public List<Device> getModeDevice(int modeById) {
        // TODO Auto-generated method stub
        List<Device> list = new ArrayList<Device>();
        try{
            String sql = "select ml.*,d.DeviceID,d.DeviceIO,d.StartTime,d.EndTime,d.DeviceName,p.ProductsName,p.ProductsCode,p.ActionShow,p.FunctionShow,p.ActionPramShow,p.FunctionParamShow, DT.DeviceTypeName FROM t_mode_list ml left join t_device d on ml.DeviceId=d.Id left join t_products p on d.deviceProductId=p.Id LEFT JOIN t_device_type dt on d.DeviceTypeKey=dt.Id left join t_device_type DT on d.DeviceTypeKey=DT.Id where ml.ModeId="
                    + modeById + " order by dt.Seqencing asc";
            if(db!=null) {
                Cursor cursor = db.rawQuery(sql, null);
                if (cursor != null) {
                    int resultCount = cursor.getCount();

                    if (resultCount == 0 || !cursor.moveToFirst()) {
                        return list;
                    }
                    for (int i = 0; i < resultCount; i++) {
                        Device device = new Device();
                        device.setDeviceName(cursor.getString(cursor
                                .getColumnIndex("DeviceName")));
                        device.setDeviceTypeName(cursor.getString(cursor
                                .getColumnIndex("DeviceTypeName")));
                        device.setProductsName(cursor.getString(cursor
                                .getColumnIndex("ProductsName")));

                        device.setProductsCode(cursor.getString(cursor
                                .getColumnIndex("ProductsCode")));
                        device.setDeviceID(cursor.getString(cursor
                                .getColumnIndex("DeviceID")));
                        device.setDeviceIO(cursor.getString(cursor
                                .getColumnIndex("DeviceIO")));
                        device.setStartTime(cursor.getString(cursor
                                .getColumnIndex("StartTime")));
                        device.setEndTime(cursor.getString(cursor
                                .getColumnIndex("EndTime")));
                        device.setGradingId(cursor.getInt(cursor
                                .getColumnIndex("GradingId")));
                        device.setActionShow(cursor.getInt(cursor
                                .getColumnIndex("ActionShow")));
                        device.setFunctionShow(cursor.getInt(cursor
                                .getColumnIndex("FunctionShow")));
                        device.setActionPramShow(cursor.getInt(cursor
                                .getColumnIndex("ActionPramShow")));
                        device.setFunctionParamShow(cursor.getInt(cursor
                                .getColumnIndex("FunctionParamShow")));
                        list.add(device);
                        cursor.moveToNext();

                    }
                }
            }
        }catch (Exception e){

        }
        return list;
    }


	@Override
	public void updateModeSort(List<Mode> list) {
		for (int i = 0; i < list.size(); i++) {
			db.execSQL("update t_mode set seqencing=? where id=?",
					new Object[] { (i + 1), list.get(i).getId() });
		}
	}

	@Override
	public void updateRoomSort(List<Room> list) {
		for (int i = 0; i < list.size(); i++) {
			db.execSQL("update t_arce set seqencing=? where id=?",
					new Object[] { i, list.get(i).getId() });
		}
	}

	@Override
	public void updateRoomHide(int homePageDisplays, int id) {
		db.execSQL("update t_arce set homePageDisplays=? where id=?",
				new Object[] { homePageDisplays, id });
	}

	@Override
	public List<DeviceType> getDeviceType() {
		List<DeviceType> list = new ArrayList<DeviceType>();
		try {
			Cursor cursor = db.query(TABLE_DEVICE_TYPE, new String[] { "id",
					"deviceType_Name", "deviceType_image", "deviceType_key",
					"deviceType_Language", "deviceType_click", "isShow",
					"seqencing" }, null, null, null, null, "seqencing asc");
			int resultCount = cursor.getCount();
			try {

				if (resultCount == 0 || !cursor.moveToFirst()) {
					return list;
				}
				for (int i = 0; i < resultCount; i++) {
					DeviceType deviceType = new DeviceType();
					deviceType
							.setId(cursor.getInt(cursor.getColumnIndex("id")));
					deviceType.setDeviceType_Name(cursor.getString(cursor
							.getColumnIndex("deviceType_Name")));

					deviceType.setSeqencing(cursor.getInt(cursor
							.getColumnIndex("seqencing")));
					deviceType.setDeviceType_key(cursor.getInt(cursor
							.getColumnIndex("deviceType_key")));
					deviceType.setDeviceType_Language(cursor.getInt(cursor
							.getColumnIndex("deviceType_Language")));
					deviceType.setDeviceType_click(cursor.getInt(cursor
							.getColumnIndex("deviceType_click")));
					deviceType.setIsShow(cursor.getInt(cursor
							.getColumnIndex("isShow")));
					list.add(deviceType);
					cursor.moveToNext();
				}
			} catch (Exception e) {
			}
		} catch (Exception e) {
		}
		return list;
	}

	@Override
	public void updateDeviceTypeSort(List<DeviceType> list) {
		for (int i = 0; i < list.size(); i++) {
			db.execSQL("update t_device_type set seqencing=? where id=?",
					new Object[] { (i + 1), list.get(i).getId() });
		}
	}

	@Override
	public int deleteRoom(int id) {
		// TODO Auto-generated method stub
		int resultCount = 0;
		try {
			resultCount += db.delete(TABLE_DEVICE, "RoomId=?",
					new String[] { String.valueOf(id) });

			resultCount += db.delete(TABLE_ROOM, "Id=?",
					new String[] { String.valueOf(id) });

		} catch (Exception e) {
			// TODO: handle exception
			resultCount = -1;
		}
		return resultCount;
	}

	public int deleteRedFra(int id) {
		int resultCount = 0;
		try {
			resultCount += db.delete(TABLE_INFRARED, "FatherId=?",
					new String[] { String.valueOf(id) });

		} catch (Exception e) {
			// TODO: handle exception
			resultCount = -1;
		}
		return resultCount;
	}

	@Override
	public int deleteDevice(String deviceName) {
		// TODO Auto-generated method stub
		int resultCount = 0;
		try {

			resultCount += db.delete(TABLE_DEVICE, "DeviceName=?",
					new String[] { deviceName });

		} catch (Exception e) {
			// TODO: handle exception
			resultCount = -1;
		}
		return resultCount;
	}
	public int deleteDevice(int Id) {
		int resultCount = 0;
		try {

			resultCount += db.delete(TABLE_DEVICE, "Id=?",
					new String[] { String.valueOf(Id) });
			resultCount += db.delete(TABLE_MODE_LIST, "DeviceId=?",
					new String[] { String.valueOf(Id) });

		} catch (Exception e) {
			// TODO: handle exception
			resultCount = -1;
		}
		return resultCount;
	}

	public String getSysNowTime() {
		Date now = new Date();
		java.text.DateFormat format = new java.text.SimpleDateFormat("HH:MM:ss");
		String formatTime = format.format(now);
		return formatTime;
	}

	@Override
	public int deleteMode(int id) {
		// TODO Auto-generated method stub
		int resultCount = 0;
		try {
			resultCount += db.delete(TABLE_MODE_LIST, "ModeId=?",
					new String[] { String.valueOf(id) });

			resultCount += db.delete(TABLE_MODE, "Id=?",
					new String[] { String.valueOf(id) });

		} catch (Exception e) {
			// TODO: handle exception
			resultCount = -1;
		}
		return resultCount;
	}

	@Override
	public int deleteMode(String modeName) {
		// TODO Auto-generated method stub
		int resultCount = 0;
		try {

			resultCount += db.delete(TABLE_MODE, "ModelName=?",
					new String[] { modeName });

		} catch (Exception e) {
			// TODO: handle exception
			resultCount = -1;
		}
		return resultCount;
	}

	public int deleteModelist(int id) {
		int resultCount = 0;
		try {
			resultCount = db.delete(TABLE_MODE_LIST, "Id=?",
					new String[] { String.valueOf(id) });
		} catch (Exception e) {
			// TODO: handle exception
			resultCount = -1;
		}
		return resultCount;
	}

	public void updateCusDevice(List<CusDevice> list) {

		if (list != null && list.size() > 0) {
			for (CusDevice cusDevice : list) {
				ContentValues contentValues = new ContentValues();
				contentValues.put("id", cusDevice.getId());
				contentValues.put("deviceName", cusDevice.getDeviceName());
				contentValues.put("deviceDes", cusDevice.getDeviceDes());
				contentValues.put("deviceType", cusDevice.getDeviceType());
				contentValues.put("deviceDid", cusDevice.getDeviceDid());
				contentValues.put("deviceIo", cusDevice.getDeviceIo());
				contentValues.put("deviceTypeId", cusDevice.getDeviceTypeId());
				contentValues.put("deviceProductId",
						cusDevice.getDeviceProductId());

				try {
					Cursor cursor = db.query(TABLE_CUS_DEVICE,
							new String[] { "deviceName" }, "id=?",
							new String[] { String.valueOf(cusDevice.getId()) },
							null, null, null, null);

					if (cursor.getCount() > 0) {

						db.update(
								TABLE_CUS_DEVICE,
								contentValues,
								"id=?",
								new String[] { String.valueOf(cusDevice.getId()) });

					} else {
						db.insert(TABLE_CUS_DEVICE, null, contentValues);
					}
				} catch (Exception e) {
					// TODO: handle exception

				}
			}
		}
	}

	public int deleteSyncData(String table) {
		return db.delete(table, null, null);
	}

	public void insertProduct(Products products) {
		ContentValues contentValues = new ContentValues();
		contentValues.put("products_name", products.getProducts_name());
		contentValues.put("products_code", products.getProducts_code());
		contentValues.put("products_version", products.getProducts_version());
		contentValues.put("products_IO", 0);
		contentValues.put("products_Id", 2);
		contentValues.put("products_key", products.getProducts_key());
		contentValues.put("actionShow", 1);
		contentValues.put("functionShow", 0);
		contentValues.put("actionPramShow", 0);
		contentValues.put("functionParamShow", 0);
		try {
			Cursor cursor = db
					.query(TABLE_PRODUCTS, new String[] { "products_name" },
							"products_key=?", new String[] { String
									.valueOf(products.getProducts_key()) },
							null, null, null, null);

			if (cursor.getCount() == 0) {

				db.insert(TABLE_PRODUCTS, null, contentValues);
			}
		} catch (Exception e) {
			// TODO: handle exception

		}
	}

	public int updateSubscribe(SubscribeInfo subscribeInfo) {
		int num = 0;
		ContentValues contentValues = new ContentValues();
		contentValues.put("subscribeName", subscribeInfo.getSubscribeName());
		contentValues.put("subscribeDate", subscribeInfo.getSubscribeDate());
		contentValues.put("subscribeTime", subscribeInfo.getSubscribeTime());
		contentValues.put("subscribeEvent", subscribeInfo.getSubscribeEvent());
		contentValues
				.put("subscribeAction", subscribeInfo.getSubscribeAction());
		contentValues.put("userName", subscribeInfo.getUserName());
		try {
			num = db.update(TABLE_SUBSCRIBE, contentValues, "id=?",
					new String[] { String.valueOf(subscribeInfo.getId()) });

		} catch (Exception e) {
			// TODO: handle exception
			System.out.println(e.toString());
			num = -1;
		}
		return num;

	}

	public int insertSubscribe(SubscribeInfo subscribeInfo) {
		int num = -1;
		try {
			ContentValues contentValues = new ContentValues();
			contentValues
					.put("subscribeName", subscribeInfo.getSubscribeName());
			contentValues
					.put("subscribeDate", subscribeInfo.getSubscribeDate());
			contentValues
					.put("subscribeTime", subscribeInfo.getSubscribeTime());
			contentValues.put("subscribeEvent",
					subscribeInfo.getSubscribeEvent());
			contentValues.put("subscribeAction",
					subscribeInfo.getSubscribeAction());
			contentValues.put("userName", subscribeInfo.getUserName());
			db.insert(TABLE_SUBSCRIBE, null, contentValues);
			num = 1;
		} catch (Exception e) {
			num = -1;
		}
		return num;
	}

	public int deleteSubscribe(int Id) {
		int resultCount = 0;
		try {
			resultCount = db.delete(TABLE_SUBSCRIBE, "id=?",
					new String[] { String.valueOf(Id) });

		} catch (Exception e) {
			// TODO: handle exception
			resultCount = -1;
		}
		return resultCount;
	}
}
